package Threadprojects.pro3_bestMatching.data.grounp;

import Threadprojects.pro3_bestMatching.data.util.BestMatchingData;
import Threadprojects.pro3_bestMatching.data.util.EditDistance;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class BestMatchingTask implements Callable<BestMatchingData> {
        String word;
        List<String> words;
        int startIndex;
        int endIndex;

    public BestMatchingTask(String word, List<String> words, int startIndex, int endIndex) {
        this.word = word;
        this.words = words;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    @Override
    public BestMatchingData call() throws Exception {
        List<String> minWords = new ArrayList<>();
        int minDistance = Integer.MAX_VALUE;
        int distance = 0;

        for (int i = startIndex; i < endIndex; i++) {
            distance = EditDistance.calculate(word, this.words.get(i));
            if (distance < minDistance) {
                minDistance = distance; // 更新最小距离
                minWords.clear();
                minWords.add(this.words.get(i));
            } else if (distance == minDistance) {
                minWords.add(this.words.get(i));
            }
        }

        return new BestMatchingData(minDistance, minWords);
    }

}
